
#include "fireskyfall.h"
#include "resourcemanager.h"
#include "gamemanager.h"
#include "frameanimation.h"
#include "CCSkeletalSprite.h"
#include "gamedatamanager.h"
#include "army.h"
#include "unit.h"
#include "gamescene.h"

static const float SKY_FALL_TIME = 0.5f;

#define __super         SkyFallBullet

FireSkyFallBullet::FireSkyFallBullet(
		Army *targetArmy,
		EquipmentAttribute *attr,
		GameScene *scene): __super(targetArmy, attr, scene) {
	_damageType = GDM->getWeaponDamageType(bulletAttribute->projectileType);

	float x = _targetArmy->frontLinePosition;
	float y = ROWS_PER_LINE / 2 * GRID_HEIGHT + FORMATION_MARGIN;

	dst = ccp(x, y);
	x -= 100.0f;
	y += 300.0f;
	src = ccp(x, y);
	position = src;

	velocity = ccp((dst.x - src.x) / SKY_FALL_TIME, (dst.y - src.y) / SKY_FALL_TIME);
}

FireSkyFallBullet::~FireSkyFallBullet() {
}

void FireSkyFallBullet::init() {
	_bolt = RM->createAnimation("fx_fire_ball", position);
	float rotation = -180.0f * ccpToAngle(velocity) / static_cast<float>(M_PI);
	_bolt->setRotation(rotation);

	_gameNodeContainer->addNode(_bolt);
}

void FireSkyFallBullet::createHitEffect() {
	for (int i = 0; i < ROWS_PER_LINE; i++) {
		CCPoint pos = ccp(dst.x, i *GRID_HEIGHT + FORMATION_MARGIN);
		FrameAnimation *fxHit = RM->createAnimation("fx_fire_hit", pos);
		fxHit->setLoop(false);
		fxHit->setAnchorPoint(ccp(0.5f, 0.0f));
		fxHit->setZOrder(static_cast<int>(GM->screenSize.height - pos.y));

		_managedGameNodeInfo.node = fxHit;
		_gameNodeContainer->addOneTimeNode(_managedGameNodeInfo);
	}
}

void FireSkyFallBullet::updateBolt(float ticks) {
	position.x += velocity.x * ticks;
	position.y += velocity.y * ticks;
	_bolt->setPosition(position);
	_bolt->update(ticks);
}

bool FireSkyFallBullet::isHitTarget() {
	return (position.y <= dst.y);
}
